<?php

/**
 *
 */
class employee extends CI_Controller{

    public function __construct(){
      parent::__construct();
      $this->load->model('M_employee');
      $this->load->helper('url');
      $this->load->helper('form');
      $this->load->library('form_validation');
      $this->load->library('session');
      if($this->session->userdata('username')==null){
        redirect('/login');
      }
      if($this->session->userdata('utype')==2){
        redirect('/home');
        return false;
      }
    }
    public function employee_list(){
      $this->load->library('pagination');
      $page = 0;
      $offset = 0;
      $per_page = 20;
      if(!empty($_GET['per_page'])){
        $page = $_GET['per_page'];
        $offset = ($page-1)*$per_page;
      }
      $result = $this->M_employee->getEmployeeList($per_page,$offset);
      if(!empty($_GET['search'])){
        $result = $this->M_employee->selectEmployee($_GET['search'],$per_page,$offset);
      }
      $config['base_url']=site_url('employee/employee_list');
      $config['total_rows'] = $result['total'];
      $config['per_page'] = $per_page;
      $this->pagination->initialize($config);
      $data['employee_list'] = $result['employeelist'];
      $data['pager'] = $this->pagination->create_links();
      $this->load->view('employee_list',$data);
    }
    public function employee_add(){
      $this->load->view('employee_add');
    }
    public function employee_add_go(){
      $vali_rules = array(
        array(
          'field' => 'emname',
          'label' => 'Emname',
          'rules' => 'required'
        ),
        array(
          'field' => 'embrand',
          'label' => 'Embrand',
          'rules' => 'required'
        ),
        array(
          'field' => 'emtel',
          'label' => 'Emtel',
          'rules' => 'required'
        ),
        array(
          'field' => 'embirth',
          'label' => 'Embirth',
          'rules' => 'required'
        ),
        array(
          'field' => 'emposition',
          'label' => 'emposition',
          'rules' => 'required'
        ),
        array(
          'field' => 'emintime',
          'label' => 'emintime',
          'rules' => 'required'
        ),
        array(
          'field' => 'emaddr',
          'label' => 'Emaddr',
          'rules' => 'required'
        )
      );
      $this->form_validation->set_rules($vali_rules);
     if ($this->form_validation->run() == FALSE) {
       echo "false";
      }else {
        $employee = array(
          'emname' => $_POST['emname'],
          'emtel' => $_POST['emtel'],
          'embrand' => $_POST['embrand'],
          'emposition' => $_POST['emposition'],
          'embirth' => $_POST['embirth'],
          'emintime' => $_POST['emintime'],
          'emaddr'=> $_POST['emaddr'],
        );
        echo $this->M_employee->addEmployee($employee);
      }
    }

    public function employee_update(){
      if(!empty($_POST['emid']) && !empty($_POST['emname']) && !empty($_POST['embrand']) && !empty($_POST['emtel']) && !empty($_POST['emposition']) && !empty($_POST['emintime']) && !empty($_POST['embirth']) && !empty($_POST['emaddr'])){
        $option = array(
          'emname'       => $_POST['emname'],
          'embrand'      => $_POST['embrand'],
          'emtel'        => $_POST['emtel'],
          'emposition'   => $_POST['emposition'],
          'emintime'     => $_POST['emintime'],
          'embirth'      => $_POST['embirth'],
          'emaddr'       => $_POST['emaddr']
        );
        echo $this->M_employee->updateEmployee($_POST['emid'],$option);
      }
    }
    public function getallemployeeselect(){
      $result = $this->M_employee->getallemployeeselect();
      print_r(json_encode($result));
    }
    public function getallemployee(){
      $page = 1;
      $offset = 0;
      $per_page = 20;
      if(!empty($_GET['per_page'])){
        $page = $_GET['per_page'];
        $offset = ($page-1)*$per_page;
      }
      $result = $this->M_employee->getallemployee($per_page,$offset);
      $result['page']=$page;
      $result['total']%$per_page==0?$result['total']=(int)($result['total']/$per_page):$result['total']=(int)($result['total']/$per_page+1);
      print_r(json_encode($result));
      // $result = $this->M_brand->getallbrand();
      // print_r(json_encode($result));
    }
    public function resetpass(){
      if(!empty($_POST['emid'])){
        if($this->M_employee->resetpass($_POST['emid'])>0){
          echo '重置成功，重置后密码为zxc123,请牢记密码';
        }else{
          echo '重置失败';
        }
      }else{
        echo '重置失败';
      }
    }
}
